// pages/mobileVerification/mobileVerification.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageType: '',                  //页面类型
    text: '验证码',                 //按钮文字
    currentTime: 61,               //倒计时
    disabled: false,               //按钮是否禁用
    mobile: '',                    //获取到的手机栏中的值
    mobileFocus: true,
    code: '',
    codeFocus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('====')
    console.log(options.pagetype)
    wx.setNavigationBarTitle({
      title: options.pagetype === 'register' ? '账号注册' 
      : options.pagetype === 'bindMobile' ? '绑定手机' 
      : options.pagetype === 'oldMobileCheck' ? '当前手机验证' 
      : options.pagetype === 'modifyPassword' ? '修改密码' : '找回密码',
    })
    if (options.pagetype === 'oldMobileCheck' 
      || options.pagetype === 'forgetPass' 
      || options.pagetype === 'modifyPassword')
    {
      this.setData({
        pageType: options.pagetype || '',
        mobile: app.globalData.userInfo?app.globalData.userInfo.mobile:''
      })
    } else {
      this.setData({
        pageType: options.pagetype || ''
      })
    }
  },

  bindMobileInput: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  bindCodeInput: function(e) {
    this.setData({
      code: e.detail.value
    })
  },

  // 下一步
  bindSubmit: function(e) {
    this.getFormId(e);
    var that = this;
    if (!this.data.mobile) {
      app.showConfirm('提示', '请输入手机号', false, function() {
        that.setData({
          mobileFocus: true
        })
      })
    } else if (!this.data.code) {
      app.showConfirm('提示', '请输入验证码', false, function() {
        that.setData({
          codeFocus: true
        })
      })
    } else {
      var data = {
        point_url: app.server.agentApi.checkSMSCode,
        mobile: that.data.mobile,
        verification: that.data.code
      }
      // 检验验证码
      app.server.getConnect(data).then(function(res) {
        // 验证码校验成功
        if(res.data.result===0){
          // 绑定手机号时
          if (that.data.pageType ==='bindMobile'){
            var data2 = {
              point_url: app.server.agentApi.modifyMobile,
              mobile: that.data.mobile,
            }
            app.server.getConnect(data2).then(function(res2){
              wx.showToast({
                title: '绑定成功',
                mask: true
              })
              setTimeout(function () {
                wx.navigateBack({})
              }, 1000)
            }, function (res) {
              app.showConfirm('提示', res.data.msg, false, function () {
                that.setData({
                  codeFocus: true,
                  code: '',
                })
              })
            })
          // 当前手机验证
          } else if (that.data.pageType === 'oldMobileCheck'){
            wx.redirectTo({
              url: '/pages/agent/center/mobileVerification/mobileVerification?pagetype=bindMobile',
            })
          // 注册
          } else if (that.data.pageType === 'register') {
            that.bindRegister();
          // 修改密码
          } else {
            wx.navigateTo({
              url: '/pages/agent/center/passwordModify/passwordModify?pagetype=' + that.data.pageType + '&mobile=' + that.data.mobile + '&code=' + that.data.code
            })
          }
        } else {
          app.showConfirm('提示', res.msg, false)
        }
      }, function(res) {
        app.showConfirm('提示', res.data.msg, false, function() {
          that.setData({
            codeFocus: true,
            code: '',
          })
        })
      })
    }
  },

  //获取验证码按钮
  bindButtonTap: function() {
    var that = this;

    var mobile = that.data.mobile;
    var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值

    var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空
    if (mobile == '') {
      warn = "号码不能为空";
    } else if (mobile.trim().length != 11 || !/^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/.test(mobile)) {
      warn = "手机号格式不正确";
    } else {
      that.setData({
        disabled: true,
        color: '#ccc',
      })
      var data = {
        point_url: app.server.agentApi.sendSMSCode,
        id: app.globalData.userInfo?app.globalData.userInfo.id:null, 
        mobile: that.data.mobile,
        type: that.data.pageType === 'register' ? 1 : that.data.pageType === 'bindMobile' ? 3 : 2,
      };
      // 发送验证码
      app.server.getConnect(data).then(function (res) {
        if(res.data.result===0){
          //当手机号正确的时候提示用户短信验证码已经发送
          wx.showToast({
            title: '短信验证码已发送',
            icon: 'none',
            duration: 2000
          });
          //设置一分钟的倒计时
          var interval = setInterval(function () {
            currentTime--; //每执行一次让倒计时秒数减一
            that.setData({
              text: '重新获取( ' + currentTime + 's )', //按钮文字变成倒计时对应秒数
            })
            //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字
            if (currentTime <= 0) {
              clearInterval(interval);
              that.setData({
                text: '重新获取',
                currentTime: 61,
                disabled: false,
                color: '#59b550'
              })
            }
          }, 1000);
        } else {
          app.showConfirm('提示', res.msg, false, function () {
            that.setData({
              codeFocus: true,
              code: '',
              disabled: false,
              color: '#59b550'
            })
          })
        }
      })
    };
    //判断 当提示错误信息文字不为空 即手机号输入有问题时提示用户错误信息 并且提示完之后让按钮为可用状态
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })
      that.setData({
        disabled: false,
        color: '#59b550'
      })
      return;
    };
  },

  // 执行注册
  bindRegister() {
    var that = this;
    var data = wx.getStorageSync('wxUserInfo');
    data.mobile = this.data.mobile;
    data['point_url'] = app.server.agentApi.register;
    app.server.getConnect(data).then(function (res) {
      that.getUserInfo();
    })
  },

  getUserInfo: function () {
    var that = this;
    var data = {
      point_url: app.server.agentApi.getMemberDetail
    };
    app.server.getConnect(data).then(function (res) {
      wx.setStorageSync("userInfo", res.data);
      app.globalData.userInfo = res.data
      wx.navigateBack();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
